package com.osce.system.service.impl;

import java.util.List;

import com.osce.common.core.entity.entity.DeptTransferApply;
import com.osce.common.core.entity.entity.SysUser;
import com.osce.common.core.entity.model.LoginUser;
import com.osce.common.utils.DateUtils;
import com.osce.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.osce.system.service.IDeptTransferApplyService;
import com.osce.system.mapper.DeptTransferApplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static com.osce.common.utils.SecurityUtils.getLoginUser;
import static com.osce.common.utils.SecurityUtils.getUserId;

/**
 * 申请更换部门审批Service业务层处理
 *
 * @author hdj
 * @since 2025-11-05
 */
@Service
public class DeptTransferApplyServiceImpl extends ServiceImpl<DeptTransferApplyMapper, DeptTransferApply> implements IDeptTransferApplyService {

    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询申请更换部门审批
     *
     * @param id 申请更换部门审批主键
     * @return 申请更换部门审批
     */
    @Override
    public DeptTransferApply selectDeptTransferApplyById(Long id) {
        return baseMapper.selectDeptTransferApplyById(id);
    }

    /**
     * 查询申请更换部门审批列表
     *
     * @param deptTransferApply 申请更换部门审批
     * @return 申请更换部门审批
     */
    @Override
    public List<DeptTransferApply> selectDeptTransferApplyList(DeptTransferApply deptTransferApply) {
        return baseMapper.selectDeptTransferApplyList(deptTransferApply);
    }

    /**
     * 新增申请更换部门审批
     *
     * @param deptTransferApply 申请更换部门审批
     * @return 结果
     */
    @Override
    public int insertDeptTransferApply(DeptTransferApply deptTransferApply) {
                deptTransferApply.setCreateTime(DateUtils.getNowDate());
                //获取当前用户
                LoginUser loginUser = getLoginUser();
                deptTransferApply.setUserId(loginUser.getUserId());
                deptTransferApply.setCurrentDeptId(loginUser.getDeptId()); //现有部门id
                deptTransferApply.setApplyTime(DateUtils.getNowDate());
                deptTransferApply.setStatus(1);//申请：1 默认待审核
                deptTransferApply.setCreateBy(loginUser.getUsername());
            return baseMapper.insertDeptTransferApply(deptTransferApply);
    }

    /**
     * 修改申请更换部门审批
     *
     * @param deptTransferApply 申请更换部门审批
     * @return 结果
     */
    @Override
    public int updateDeptTransferApply(DeptTransferApply deptTransferApply) {
        // 审核状态：1-待审核，2-已通过，3-已驳回
        if (deptTransferApply.getStatus() != 3) {
            deptTransferApply.setReasonsRejection("");
        }

        // 数据的更新时间
        deptTransferApply.setUpdateTime(DateUtils.getNowDate());
        // 设置审批人ID
        deptTransferApply.setApproverId(getUserId());
        // 设置审批人姓名
        deptTransferApply.setApproverName(getLoginUser().getUsername());
        // 设置审批时间
        deptTransferApply.setApprovalTime(DateUtils.getNowDate());

        int i = baseMapper.updateDeptTransferApply(deptTransferApply);

        if (i > 0 && deptTransferApply.getStatus() == 2) {
            // 已通过，更新用户部门
            updateUserDept(deptTransferApply.getUserId(), deptTransferApply.getTargetDeptId());
        }
        return i;
    }

    public void updateUserDept(Long userId, Long targetDeptId){
        SysUser user = new SysUser();
        user.setUserId(userId);
        user.setDeptId(targetDeptId);
        sysUserMapper.updateUserDept(user);
    }

    /**
     * 批量删除申请更换部门审批
     *
     * @param ids 需要删除的申请更换部门审批主键
     * @return 结果
     */
    @Override
    public int deleteDeptTransferApplyByIds(Long[] ids) {
        return baseMapper.deleteDeptTransferApplyByIds(ids);
    }

    /**
     * 删除申请更换部门审批信息
     *
     * @param id 申请更换部门审批主键
     * @return 结果
     */
    @Override
    public int deleteDeptTransferApplyById(Long id) {
        return baseMapper.deleteDeptTransferApplyById(id);
    }
}
